var network = require('../utils/network_util.js');
var api = require('../utils/api.js');
var utils = require('../utils/utils.js');

// 点击bananer
function onBananerItemClick(target, e) {
  onItemClick(target, e);
}

// 点击按钮导航
function onNavigateItemClick(target, e) {
  onItemClick(target, e);
}

// 点击推荐商品
function onRecommendItemClick(target, e) {
  onItemClick(target, e);
}

// 点击拼团商品
function onSpellProductItemClick(target, e) {
  onItemClick(target, e);
}

// 点击文章
function onArticleItemClick(target, e) {
  onItemClick(target, e);
}

// 点击自适应图片
function onImageHItemClick(target, e) {
  onItemClick(target, e);
}

//点击领取优惠券
function clickhongbao(target, e) {
  var item = e.currentTarget.dataset.coupon;
  var couponId = [];
  for (var i = 0; i < item.length; i++) {
    couponId.push(item[i].id);
  }
  console.log(couponId);
  network._post_json(target, api.receiveCoupon, { couponId }, success);
  target.setData({
    isyouhuishow: true
  })
}

// 关闭弹屏
function powerDrawer(target, e) {

  target.setData({
    showModalStatus: false
  });
}

// 点击弹屏广告
function onAdvertItemClick(target, e) {
  target.setData({
    showModalStatus: false
  });
  onItemClick(target, e);
}

/**
 * 所有商品 0 (旧数据) 
 * 商品详情 1 
 * 商品分类 2 
 * 优惠券列表 3 
 * 商品搜索 4 
 * 到店自提 5(旧数据) 
 * 拼团商品 6
 * 文章详情 7 
 * 文章分类 8 
 * 拼团列表 9
 * 积分商品列表 10 
 * 积分商品详情 11 
 * 分销页面 12 
 * 门店列表 13 
 * 门店详情 14
 */
// 点击Item
function onItemClick(target, e) {
  if (target.data.utils.canTap(e)) {
    var item = e.currentTarget.dataset.item;
    if (item.linkType == 0) {
      wx.navigateTo({
        url: '/pages/product/Product'
      })
    } else if (item.linkType == 1) {
      if (item.linkId) {
        wx.navigateTo({
          url: '/pages/product/ProductDetails?productId=' + item.linkId
        })
      }
    } else if (item.linkType == 2) {
      if (item.linkId) {
        wx.setStorageSync("linkId", item.linkId);
        wx.switchTab({
          url: '/pages/product/Product'
        })
      }
    } else if (item.linkType == 3) {
      wx.navigateTo({
        url: '/pages/myself/coupon/TenantCoupon',
      })
    } else if (item.linkType == 4) {
      wx.navigateTo({
        url: '/pages/product/ProductSearch',
      })
    } else if (item.linkType == 5) {
      wx.navigateTo({
        url: '/pages/myself/shop/OrderShop',
      })
    } else if (item.linkType == 6) {
      if (item.linkId) {
        wx.navigateTo({
          url: '/pages/product/SpellGroupDetails?spellGroupId=' + item.linkId
        })
      }
    } else if (item.linkType == 7) {
      if (item.linkId) {
        wx.navigateTo({
          url: '/pages/article/ArticleDetails?articleId=' + item.linkId,
        })
      }
    } else if (item.linkType == 8) {
      wx.navigateTo({
        url: '/pages/article/Articles?linkId=' + item.linkId
      })
    } else if (item.linkType == 9) {
      wx.switchTab({
        url: '/pages/product/SpellGroupList'
      })
    } else if (item.linkType == 10) {
      wx.navigateTo({
        url: '/pages/myself/integral/Integral',
      })
    } else if (item.linkType == 11) {
      if (item.linkId) {
        wx.navigateTo({
          url: '/pages/myself/integral/RedeemDetails?ipsId=' + item.linkId
        })
      }
    } else if (item.linkType == 12) {
      wx.navigateTo({
        url: '/pages/myself/commission/Qrcode',
      })
    } else if (item.linkType == 13) {
      wx.navigateTo({
        url: '/pages/myself/store/StoreInfo',
      })
    } else if (item.linkType == 14) {
      if (item.linkId) {
        wx.navigateTo({
          url: '/pages/myself/store/StoreDetails?linkId=' + item.linkId
        })
      }
    }
  }
}

// 领取优惠券
function receiveCoupon(target, e) {
  var item = e.currentTarget.dataset.item;
  network._post_json(target, api.receiveCoupon, { counponId: item.id }, success);
}

// 获取优惠券列表结果
var success = function (that, res) {
  if (res.data.data) {
    if (res.data.data == '已领完') {
      // wx.showToast({
      //   title: '已领完'
      // })
      that.showZanTopTips('已领完');
    } else if (res.data.data == '存在已下架优惠券') {
      that.showZanTopTips('存在已下架优惠券，请刷新');
    }
    else {
      wx.showToast({
        title: '领取成功'
      })
    }
  }
}

module.exports = {
  onBananerItemClick: onBananerItemClick,
  onNavigateItemClick: onNavigateItemClick,
  onRecommendItemClick: onRecommendItemClick,
  onSpellProductItemClick: onSpellProductItemClick,
  onArticleItemClick: onArticleItemClick,
  powerDrawer: powerDrawer,
  onAdvertItemClick: onAdvertItemClick,
  onImageHItemClick: onImageHItemClick,
  receiveCoupon: receiveCoupon,
  clickhongbao: clickhongbao
}